package com.twentyOne.ClothingLand.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.twentyOne.ClothingLand.service.IShopService;
import com.twentyOne.ClothingLand.service.impl.ShopService;
import com.twentyOne.ClothingLand.util.JsonUtil;
import com.twentyOne.ClothingLand.vo.Shop;

public class ShopAction extends ActionSupport {

	private Shop shop;
	private IShopService shopService = new ShopService();

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public String addShop() throws Exception {

		this.shopService.addShop(shop);
		return null;

	}

	public String deleteShop() throws Exception {

		this.shopService.deleteShop(shop);
		return null;

	}

	public String modifyShop() throws Exception {

		this.shopService.modifyShop(shop);
		return null;

	}

	public String searchAllShop() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		List<Shop> list = this.shopService.searchAllShop();

		if (list != null) {
			String shopsJson = JsonUtil.objectsToJson(list);
			out.print(shopsJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

	public String searchShopByID() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		Shop shop_ = this.shopService.searchShopByID(shop.getShopId());

		if (shop_ != null) {
			String shopJson = JsonUtil.objectToJson(shop_);
			out.print(shopJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

}
